package com.moweilin.mo_gateway.handlers;

import com.moweilin.mo_gateway.services.IModuleService;
import io.vertx.core.Handler;
import io.vertx.core.http.HttpServerRequest;

/**
 * Created by moweilin on 2017/2/21.
 */
public class DefaultDispatcherHandler implements Handler<HttpServerRequest> {

    private IModuleService moduleService;
    private Handler<HttpServerRequest>  localRequestHandler;
    private IModuleRequestHandler  moduleRequestHandler;


    public DefaultDispatcherHandler(IModuleService moduleService, Handler<HttpServerRequest> localRequestHandler, IModuleRequestHandler moduleRequestHandler) {
        this.moduleService = moduleService;
        this.localRequestHandler = localRequestHandler;
        this.moduleRequestHandler = moduleRequestHandler;
    }

    /**
     * @param request
     * If no module found at {@link IModuleService} for the request, {@link DefaultLocalRequestHandler} will handle request default;
     */
    @Override
    public final void handle(HttpServerRequest request) {
        ModuleRequest moduleRequest = new ModuleRequest(moduleService,request);
        if(moduleRequest.getModule() != null){
            moduleRequestHandler.handle(moduleRequest);
        }else {
            localRequestHandler.handle(request);
        }
    }
}
